package com.wsx.java.boddy.agent;

import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.pool.TypePool;
import net.bytebuddy.utility.JavaModule;

/**
 * @author wangshuaixin
 * @Title: com.wsx.java.boddy.agent.AbstractTransform
 * @Description: TODO
 * @date 2019/02/19
 */
public abstract class AbstractTransform implements AgentBuilder.Transformer {

    private final String inteceptor;

    public AbstractTransform(String inteceptor) {
        this.inteceptor = inteceptor;
    }

    @Override
    public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader, JavaModule module) {
        ClassFileLocator.Compound compound = new ClassFileLocator.Compound(ClassFileLocator.ForClassLoader.of(classLoader),
                ClassFileLocator.ForClassLoader.ofClassPath());
        return builderTransform(builder, compound).intercept(MethodDelegation.to(TypePool.Default.of(compound).describe(inteceptor).resolve()));
    }

    protected abstract DynamicType.Builder.MethodDefinition.ImplementationDefinition builderTransform(DynamicType.Builder<?> builder,  ClassFileLocator.Compound compound);

}
